package com.cyz.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cyz.dao.AdminDao;
import com.cyz.pojo.Admin;
import com.cyz.pojo.Product;
import com.cyz.service.AdminService;
import com.cyz.util.DBOperation;
import com.cyz.util.MD5Util;

public class AdminDaoImpl implements AdminDao {

	/**
	 * 获取所有管理员信息
	 *
	 * @param
	 * @return
	 */
	@Override
	public List<Admin> getAllAdmin() {
		List<Admin> adminList=new ArrayList<Admin>();
		ResultSet rs = null;
		String sql=	"select * from admin";
		List<Object> param = new ArrayList<Object>();
		rs = DBOperation.query(sql, param); 
		try {
			while(rs.next())
			{
				Admin admin=new Admin();
				admin.setId(rs.getInt("id"));
				admin.setAdminName(rs.getString("adminName"));
				admin.setPassword(rs.getString("password"));
				admin.setStatus(rs.getInt("status"));
				admin.setRole(rs.getString("role"));
				admin.setMobile(rs.getString("mobile"));
				adminList.add(admin);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	return adminList;
	}
/**
 * 
 *通过名字来获取角色
 * @param
 * @return
 */
	@Override
	public String getRoleByName(String adminName) {
		String role=null;
		ResultSet rs = null;
		String sql=	"select role from admin where adminName=?";
		List<Object> param = new ArrayList<Object>();
		param.add(adminName);	
		rs = DBOperation.query(sql, param); 
		try {
			while(rs.next())
			{
				try {
					role=rs.getString("role");
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	
		return role;
	}
/**
 * 
 *通过名字获取用户ID
 * @param
 * @return
 */
	@Override
	public Integer getAdminIdByName(String adminName) {
		Integer id=null;
		ResultSet rs = null;
		String sql="select id from admin where adminName=?";
		List<Object> param = new ArrayList<Object>();
		param.add(adminName);
		rs = DBOperation.query(sql, param);
		try {
			while(rs.next())
			{
				//Admin admin=new Admin();
				id=rs.getInt("id");
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return id;
	}
/**
 * 
 *更新管理员信息
 * @param
 * @return
 */
	@Override
	public void updateAdmin(Admin admin) {
		String sql="update admin set adminName=?,mobile=?,password=md5(?) ,status=? ,role=? where id=?";
		List<Object> param = new ArrayList<Object>();
		param.add(admin.getAdminName());
		param.add(admin.getMobile());
		param.add(admin.getPassword());
		param.add(admin.getStatus());
		param.add(admin.getRole());
		param.add(admin.getId());
	    try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	
	}
/**
 * 新增管理员
 *
 * @param
 * @return
 */
	@Override
	public void addAdmin(Admin admin) {
		String sql="insert into admin (adminName,password,status,role,mobile) values(?,?,?,?,?)";
		List<Object> param = new ArrayList<Object>();
		param.add(admin.getAdminName());	
		param.add(MD5Util.toMd5(admin.getPassword()));
		param.add(admin.getStatus());
		param.add(admin.getRole());
		param.add(admin.getMobile());
	    try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
/**
 * 通过管理员名字来获取管理员对象
 *
 * @param
 * @return
 */
	@Override
	public Admin findAdminByName(String name) {
		Admin admin = null;
		ResultSet rs = null;
		String sql=	"select * from admin where adminName=?";
		List<Object> param = new ArrayList<Object>();
		param.add(name);
		rs = DBOperation.query(sql, param); 
		try {
			while(rs.next())
			{
				admin=new Admin();
				admin.setId(rs.getInt("id"));
				admin.setAdminName(rs.getString("adminName"));
				admin.setPassword(rs.getString("password"));
				admin.setStatus(rs.getInt("status"));
				admin.setRole(rs.getString("role"));
				admin.setMobile(rs.getString("mobile"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	return admin;

	}
/**
 * 通过id来获取管理员对象
 *
 * @param
 * @return
 */
	@Override
	public Admin getAdminById(Integer adminId) {
		Admin admin=new Admin();
		ResultSet rs = null;
		String sql=	"select * from admin where id=?";
		List<Object> param = new ArrayList<Object>();
		param.add(adminId);
		rs = DBOperation.query(sql, param); 
		try {
			while(rs.next())
			{
				admin.setId(rs.getInt("id"));
				admin.setAdminName(rs.getString("adminName"));
				admin.setPassword(rs.getString("password"));
				admin.setStatus(rs.getInt("status"));
				admin.setRole(rs.getString("role"));
				admin.setMobile(rs.getString("mobile"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	return admin;
	}

/**
 * 禁用管理员
 *
 * @param
 * @return
 */
	@Override
	public void stopByName(String adminName) {
		String sql=	"update admin set status =0 where adminName=?";
		List<Object> param = new ArrayList<Object>();
		param.add(adminName);
		try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
	}
/**
 * 启用管理员
 *
 * @param
 * @return
 */
	@Override
	public void startByName(String adminName) {
		String sql=	"update admin set status =1 where adminName=?";
		List<Object> param = new ArrayList<Object>();
		param.add(adminName);
		try {
			DBOperation.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
	}
	
}
